//
// Created by dywei on 2021/7/6.
//

#include <jni.h>
#include <android/log.h>
#include "client/linux/handler/minidump_descriptor.h"
#include "client/linux/handler/exception_handler.h"

bool DumpCallback(const google_breakpad::MinidumpDescriptor &descriptor,
                  void *context,
                  bool succeeded) {
    __android_log_print(ANDROID_LOG_ERROR, "native", "native crash:%s", descriptor.path());
    return false;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_dyw_crashcatch_NativeCrash_initNativeCrash(JNIEnv
*env,
jclass clazz, jstring
path_) {
const char *path = env->GetStringUTFChars(path_, 0);

__android_log_print(ANDROID_LOG_INFO,
"native", "===> %s", path);

google_breakpad::MinidumpDescriptor descriptor(path);
static google_breakpad::ExceptionHandler eh(descriptor, NULL, DumpCallback,
                                            NULL, true, -1);

env->
ReleaseStringUTFChars(path_, path
);

}

extern "C"
JNIEXPORT void JNICALL
Java_com_dyw_crashcatch_NativeCrash_triggerNativeCrash(JNIEnv *env, jclass clazz) {

__android_log_print(ANDROID_LOG_INFO, "native", "发生崩溃");

int *p = NULL;
*p = 10;

}